//
//  YCGoodsCenterViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/3/2.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCGoodsCenterViewController.h"
#define YCSortCellIdentify @"YCSortTableViewCell"
#define YCGoodsCellIdentify @"YCGoodsTableViewCell"

@interface YCGoodsCenterViewController ()
{
    UITableView *m_leftTableView;
    UITableView *m_tableView;
    NSInteger _selectIndex;
    
    YCStockAlertView *m_stockView;
    NSMutableArray *m_arraySorts;
}
@end

@implementation YCGoodsCenterViewController
-(void)viewWillAppear:(BOOL)animated
{
    [_naviVC setNaviHidden:NO];
    [_naviVC setMainNaviVC];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"中央产品库";
    self.navigationItem.leftBarButtonItem=[self tbarBackButtonWhite];
    
     _selectIndex=0;
    [self addMianView];
    [self loadData];
}
-(void)loadData
{
    ItemIndexRequest *request = [ItemIndexRequest new];
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    [apiClient doItemIndex:request success:^(ApiResponse *data, NSString *url) {
        ItemIndexResponse *response=[[ItemIndexResponse alloc]initWithCGResponse:data];
        m_arraySorts=[NSMutableArray arrayWithArray:response.list];
        
        [m_leftTableView reloadData];
        [m_tableView reloadData];
    }failure:^(ApiResponse *data, NSString *url){
    }];
}
-(void)addMianView
{
    m_leftTableView=[[UITableView alloc]init];
    m_leftTableView.backgroundColor=BACK_COLOR;
    m_leftTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    m_leftTableView.delegate=self;
    m_leftTableView.dataSource=self;
    m_leftTableView.bounces=NO;
    [self.view addSubview:m_leftTableView];
    [m_leftTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.width.mas_equalTo(50);
        make.top.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(0);
    }];
    [m_leftTableView registerNib:[UINib nibWithNibName:YCSortCellIdentify bundle:nil] forCellReuseIdentifier:YCSortCellIdentify];
    
    m_tableView=[[UITableView alloc]init];
    m_tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    m_tableView.delegate=self;
    m_tableView.dataSource=self;
    [self.view addSubview:m_tableView];
    [m_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(m_leftTableView.right).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(kNAV_HEIGHT);
        make.bottom.equalTo(self.view).offset(0);
    }];
    [m_tableView registerNib:[UINib nibWithNibName:YCGoodsCellIdentify bundle:nil] forCellReuseIdentifier:YCGoodsCellIdentify];
    
    UIView *header=[[UIView alloc]initWithFrame:CGRectMake(0, 0, W(m_tableView), kSmallDistance)];
    m_tableView.tableHeaderView=header;
}
#pragma mark tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView==m_leftTableView) {
        return 1;
    }
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==m_leftTableView) {
        return m_arraySorts.count;
    }
    if (m_arraySorts.count==0) {
        return 0;
    }else{
        ItemSortTable *sort=m_arraySorts[_selectIndex];
        return sort.items.count;
    }
}
#pragma mark tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==m_leftTableView) {
        ItemSortTable *site=m_arraySorts[indexPath.row];
        CGFloat height=getTextSize(site.title, 35, [UIFont LightFontOfSize:14]).height-45;
        return 70+height;
    }
    return 120;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if (tableView==m_leftTableView) {
        YCSortTableViewCell *_cell = (YCSortTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCSortCellIdentify forIndexPath:indexPath];
        if (indexPath.row==_selectIndex) {
            [_cell setCellSelected:YES];
        }else{
            [_cell setCellSelected:NO];
        }
        [_cell bindWith:m_arraySorts[indexPath.row]];
        cell=_cell;
    }else{
        YCGoodsTableViewCell *_cell = (YCGoodsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCGoodsCellIdentify forIndexPath:indexPath];
        _cell.index=3;
        ItemSortTable *sort=m_arraySorts[_selectIndex];
        [_cell bindWith:sort.items[indexPath.row]];
        _cell.addBlock=^(ItemTable *item){
            [self showStockAlertWith:item];
        };
        cell=_cell;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView==m_leftTableView) {
        _selectIndex=indexPath.row;
        [m_leftTableView reloadData];
        [m_tableView reloadData];
    }else{
        ItemSortTable *sort=m_arraySorts[_selectIndex];
        ItemTable *item=sort.items[indexPath.row];
        YCGoodsDetailViewController *goodsVC=[[YCGoodsDetailViewController alloc]init];
        goodsVC.itemId=item.Id;
        [self showNavigationView:goodsVC];
    }
}
-(void)showStockAlertWith:(ItemTable *)item
{
    if (m_stockView==nil) {
        m_stockView=[[YCStockAlertView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainScreen_Height)];
    }
    [m_stockView showAlertWith:item];
    m_stockView.cancelBlock=^(){
        
    };
    m_stockView.confirmBlock=^(NSString *num){
        item.stock=num;
        [self loadAddDataWith:item];
    };
    [self.view.window addSubview:m_stockView];
}
-(void)loadAddDataWith:(ItemTable *)item
{
    ShopItemCreatRequest *request = [ShopItemCreatRequest new];
    [apiClient hideProgress];
    request.num=item.stock;
    request.item_id=item.Id;
    [apiClient doShopItemCreat:request success:^(ApiResponse *data, NSString *url) {
        [self loadData];
        [m_leftTableView reloadData];
        [m_tableView reloadData];
        self.addBlock();
    }failure:^(ApiResponse *data, NSString *url){
    }];
}

@end
